package com.laicunba.service.jzh.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.laicunba.constant.GloabConstant;

public class JzhBaseService {

	protected String mchntCd = GloabConstant.JZH_MCHNT_CD;
	protected String mchntURL = GloabConstant.JZH_MCHNT_URL;

	protected Document parseXml(byte[] bytes) throws ParserConfigurationException, SAXException, IOException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		builder = factory.newDocumentBuilder();
		Document doc = builder.parse(new ByteArrayInputStream(bytes));
		return doc;
	}

	protected String getValue(Document doc, String tagName) {
		NodeList nodeList = doc.getElementsByTagName(tagName);
		if (nodeList == null || nodeList.getLength() == 0) {
			return null;
		}
		Element e = (Element) nodeList.item(0);
		return e.getTextContent();
	}

	protected String getValue(Element doc, String tagName) {
		NodeList nodeList = doc.getElementsByTagName(tagName);
		if (nodeList == null || nodeList.getLength() == 0) {
			return null;
		}
		Element e = (Element) nodeList.item(0);
		return e.getTextContent();
	}

	protected NodeList getElementsByTagName(Document doc, String tagName) {
		NodeList nodeList = doc.getElementsByTagName(tagName);
		if (nodeList == null || nodeList.getLength() == 0) {
			return null;
		}
		return nodeList;
	}

}
